<?php

/**
 ** added by lss@2017/10/18
 ** workerman 的 公用controller 类
 **/
namespace app\mobile\controller;
use app\common\model\SystemSet;
use think\worker\Server;
use Workerman\Lib\Timer;
use think\Db;
use app\common\logic\Pocessing;

header("Access-Control-Allow-Origin: *");

class Workerman extends Server
{

    protected $socket = 'websocket://0.0.0.0:2120';

    protected $lot_data_logic = [];
    protected $lot_data_model = [];
    protected $timerobj = null;
    public function __construct(){
        parent::__construct();
    }

    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $message){

        //gettype($message);
        // 客户端传递的是json数据
        $connection->lastMessageTime = time();
        $this->lot_data_logic = new Pocessing();
        $message_data = json_decode($message, true);

//        $this->worker->uidConnections[]=$connection;

        if(!$message_data){
            return ;
        }

        switch($message_data['type']){

            case 'getAwardTime':  //初进入页面的操作
                $lotType = $message_data['client_name'];
               
                $timer_id = Timer::add(1, function()use(&$timer_id,$connection,$lotType){
                	$recent_lot_data = $this->lot_data_logic->asyncDataAll($lotType);
                	$return_data = array('type'=>'getAwardTime','data'=>$recent_lot_data);
                	if($recent_lot_data['next']['jztime']>0){
                		Timer::del($timer_id);
                		$connection->send(json_encode($return_data));
                	}
                });
                
                break;
            case 'getAwardData';
                $time_interval = '1';
                $lotType = $message_data['client_name'];
                $nextIssue = $message_data['next'];
                $timer_id = Timer::add($time_interval, function()use(&$timer_id,$connection, $nextIssue,$lotType){
                    $kjjg = $this->lot_data_logic->asyncDataDetail($lotType,$nextIssue);
                    if( $kjjg ){
                        $return_data = array('type'=>'getRealData','data'=>$kjjg);
                        Timer::del($timer_id);
                        $connection->send(json_encode($return_data));
                    }
                });
                break;
        }


    }
    public static function voiceRemind( $worker ){

        $systemSet = new SystemSet();
        $mic_xx = $systemSet->dLists('xt_tishi_msic');
        $xxmic='';
        $xsmic='';
        $txmic='';

        $xxcz_class = 0;
        $xscz_class = 0;
        $tx_class = 0;

        $xxcz_video = '';
        $xscz_video = '';
        $tx_video = '';

        foreach ($mic_xx as $mic_v){
            if(empty($xxmic) && $mic_v['xxcz']==1 && $mic_v['xxcz_open']==1 ){
                $xxmic='../../static/misc/'.$mic_v['mp3_lj'];
                $xxcz_class = intval( $mic_v['xxcz_class'] );
            }
            if(empty($xsmic) && $mic_v['xscz']==1 && $mic_v['xscz_open']==1 ){
                $xsmic='../../static/misc/'.$mic_v['mp3_lj'];
                $xscz_class = intval( $mic_v['xscz_class'] );
            }
            if(empty($txmic) && $mic_v['tx']==1 &&$mic_v['tx_open']==1 ){
                $txmic='../../static/misc/'.$mic_v['mp3_lj'];
                $tx_class = intval( $mic_v['tx_class'] );
            }
        }
        if($xxmic != ''){
            $where = 'czclass=2 and zt<1 and is_read=0';
            $mic = SystemSet::name('chongzhi_list')->where($where)->find();
            if( !empty($mic) ){
                if( SystemSet::name('chongzhi_list')->where($where)->update(array('is_read'=>1)) ){
                    $xxcz_video = "<embed autoplay='false' src='{$xxmic}'  width='0' height='0' />";
                }
            }
        }
        if($xsmic != ''){
            $where = 'czclass=1 and zt<1 and is_read=0';

            $mic=SystemSet::name('chongzhi_list')->where($where)->find();
            if( !empty($mic) ){
                if( SystemSet::name('chongzhi_list')->where($where)->update(array('is_read'=>1)) ){
                    $xscz_video = "<embed autoplay='false' src='{$xsmic}'  width='0' height='0' />";
                }
            }
        }
        if($txmic != ''){
            $where = 'zt<1 and ifck<1 and is_read=0';
            $mic = SystemSet::name('tixian_list')->where($where)->find();
            if( !empty($mic) ){
                if( SystemSet::name('tixian_list')->where($where)->update(array('is_read'=>1)) ){
                    $tx_video = "<embed autoplay='false' src='{$txmic}'  width='0' height='0' />";
                }
            }
        }
        $ret = array();
        if( !empty($xxcz_video) && $xxcz_class > 0 ){
            $ret['xx'] = array($xxcz_class=>$xxcz_video);
        }
        if( !empty($xscz_video) && $xscz_class > 0 ){
            $ret['xs'] = array($xscz_class=>$xscz_video);
        }
        if( !empty($tx_video) && $tx_class > 0 ){
            $ret['tx'] = array($tx_class=>$tx_video);
        }
        if(!empty($ret)){
            if(!empty($worker->uidConnections)){
                foreach($worker->uidConnections as $connection)
                {
                    $connection->send(json_encode($ret));
                }
            }
        }

    }

    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection)
    {
        $connection_data=array('type'=>'connect','msg'=>'success');
        $connection_data = json_encode($connection_data);

        $connection->send($connection_data);
    }

    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection)
    {
        $connection->close();
        echo 'has closed';
    }

    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg)
    {
        $error_data=array('type'=>'error','msg'=>'uncaught error');
        $error_data = json_encode($error_data);
        $error_data->send($error_data);
    }

    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {
        /*
        Timer::add(1, function ()use($worker)
        {
            //关闭失联连接
            $time_now = time();
            foreach ($worker->connections as $connection)
            {
                // 有可能该connection还没收到过消息，则lastMessageTime设置为当前时间
                if (empty($connection->lastMessageTime))
                {
                    $connection->lastMessageTime = $time_now;
                    continue;
                }
                // 上次通讯时间间隔大于心跳间隔，则认为客户端已经下线，关闭连接
                if ($time_now - $connection->lastMessageTime > 60*5)
                {
                    $connection->close();
                }
            }
        });
        */
    }
}
